home *** CD-ROM | disk | FTP | other *** search
/ Champak 141 / (Vol 141) Oct 17 2011.iso / Games / despereaux-swings.swf / scripts / Box2D / Dynamics / b2Island.as < prev    next >
Encoding:
Text File  |  2011-10-17  |  12.0 KB  |  352 lines

  1. package Box2D.Dynamics
  2. {
  3.    import Box2D.Collision.b2Manifold;
  4.    import Box2D.Collision.b2ManifoldPoint;
  5.    import Box2D.Common.Math.b2Mat22;
  6.    import Box2D.Common.Math.b2Math;
  7.    import Box2D.Common.Math.b2Vec2;
  8.    import Box2D.Common.b2Settings;
  9.    import Box2D.Dynamics.Contacts.b2Contact;
  10.    import Box2D.Dynamics.Contacts.b2ContactConstraint;
  11.    import Box2D.Dynamics.Contacts.b2ContactConstraintPoint;
  12.    import Box2D.Dynamics.Contacts.b2ContactResult;
  13.    import Box2D.Dynamics.Contacts.b2ContactSolver;
  14.    import Box2D.Dynamics.Joints.b2Joint;
  15.    
  16.    public class b2Island
  17.    {
  18.       
  19.       private static var s_reportCR:b2ContactResult = new b2ContactResult();
  20.        
  21.       
  22.       public var m_joints:Array;
  23.       
  24.       public var m_listener:b2ContactListener;
  25.       
  26.       public var m_positionIterationCount:int;
  27.       
  28.       public var m_bodyCapacity:int;
  29.       
  30.       public var m_bodies:Array;
  31.       
  32.       public var m_jointCapacity:int;
  33.       
  34.       public var m_contactCount:int;
  35.       
  36.       public var m_contacts:Array;
  37.       
  38.       public var m_contactCapacity:int;
  39.       
  40.       public var m_jointCount:int;
  41.       
  42.       public var m_allocator:*;
  43.       
  44.       public var m_bodyCount:int;
  45.       
  46.       public function b2Island(param1:int, param2:int, param3:int, param4:*, param5:b2ContactListener)
  47.       {
  48.          var _loc6_:int = 0;
  49.          super();
  50.          m_bodyCapacity = param1;
  51.          m_contactCapacity = param2;
  52.          m_jointCapacity = param3;
  53.          m_bodyCount = 0;
  54.          m_contactCount = 0;
  55.          m_jointCount = 0;
  56.          m_allocator = param4;
  57.          m_listener = param5;
  58.          m_bodies = new Array(param1);
  59.          _loc6_ = 0;
  60.          while(_loc6_ < param1)
  61.          {
  62.             m_bodies[_loc6_] = null;
  63.             _loc6_++;
  64.          }
  65.          m_contacts = new Array(param2);
  66.          _loc6_ = 0;
  67.          while(_loc6_ < param2)
  68.          {
  69.             m_contacts[_loc6_] = null;
  70.             _loc6_++;
  71.          }
  72.          m_joints = new Array(param3);
  73.          _loc6_ = 0;
  74.          while(_loc6_ < param3)
  75.          {
  76.             m_joints[_loc6_] = null;
  77.             _loc6_++;
  78.          }
  79.          m_positionIterationCount = 0;
  80.       }
  81.       
  82.       public function AddBody(param1:b2Body) : void
  83.       {
  84.          var _loc2_:* = m_bodyCount++;
  85.          m_bodies[_loc2_] = param1;
  86.       }
  87.       
  88.       public function AddJoint(param1:b2Joint) : void
  89.       {
  90.          var _loc2_:* = m_jointCount++;
  91.          m_joints[_loc2_] = param1;
  92.       }
  93.       
  94.       public function Report(param1:Array) : void
  95.       {
  96.          var _loc2_:b2Mat22 = null;
  97.          var _loc3_:b2Vec2 = null;
  98.          var _loc4_:int = 0;
  99.          var _loc5_:b2Contact = null;
  100.          var _loc6_:b2ContactConstraint = null;
  101.          var _loc7_:b2ContactResult = null;
  102.          var _loc8_:b2Body = null;
  103.          var _loc9_:int = 0;
  104.          var _loc10_:Array = null;
  105.          var _loc11_:int = 0;
  106.          var _loc12_:b2Manifold = null;
  107.          var _loc13_:int = 0;
  108.          var _loc14_:b2ManifoldPoint = null;
  109.          var _loc15_:b2ContactConstraintPoint = null;
  110.          if(m_listener == null)
  111.          {
  112.             return;
  113.          }
  114.          _loc4_ = 0;
  115.          while(_loc4_ < m_contactCount)
  116.          {
  117.             _loc5_ = m_contacts[_loc4_];
  118.             _loc6_ = param1[_loc4_];
  119.             (_loc7_ = s_reportCR).shape1 = _loc5_.m_shape1;
  120.             _loc7_.shape2 = _loc5_.m_shape2;
  121.             _loc8_ = _loc7_.shape1.m_body;
  122.             _loc9_ = _loc5_.m_manifoldCount;
  123.             _loc10_ = _loc5_.GetManifolds();
  124.             _loc11_ = 0;
  125.             while(_loc11_ < _loc9_)
  126.             {
  127.                _loc12_ = _loc10_[_loc11_];
  128.                _loc7_.normal.SetV(_loc12_.normal);
  129.                _loc13_ = 0;
  130.                while(_loc13_ < _loc12_.pointCount)
  131.                {
  132.                   _loc14_ = _loc12_.points[_loc13_];
  133.                   _loc15_ = _loc6_.points[_loc13_];
  134.                   _loc7_.position = _loc8_.GetWorldPoint(_loc14_.localPoint1);
  135.                   _loc7_.normalImpulse = _loc15_.normalImpulse;
  136.                   _loc7_.tangentImpulse = _loc15_.tangentImpulse;
  137.                   _loc7_.id.key = _loc14_.id.key;
  138.                   m_listener.Result(_loc7_);
  139.                   _loc13_++;
  140.                }
  141.                _loc11_++;
  142.             }
  143.             _loc4_++;
  144.          }
  145.       }
  146.       
  147.       public function Solve(param1:b2TimeStep, param2:b2Vec2, param3:Boolean, param4:Boolean) : void
  148.       {
  149.          var _loc5_:int = 0;
  150.          var _loc6_:b2Body = null;
  151.          var _loc7_:b2Joint = null;
  152.          var _loc8_:b2ContactSolver = null;
  153.          var _loc9_:int = 0;
  154.          var _loc10_:Boolean = false;
  155.          var _loc11_:Boolean = false;
  156.          var _loc12_:Boolean = false;
  157.          var _loc13_:Number = NaN;
  158.          var _loc14_:Number = NaN;
  159.          var _loc15_:Number = NaN;
  160.          _loc5_ = 0;
  161.          while(_loc5_ < m_bodyCount)
  162.          {
  163.             if(!(_loc6_ = m_bodies[_loc5_]).IsStatic())
  164.             {
  165.                _loc6_.m_linearVelocity.x += param1.dt * (param2.x + _loc6_.m_invMass * _loc6_.m_force.x);
  166.                _loc6_.m_linearVelocity.y += param1.dt * (param2.y + _loc6_.m_invMass * _loc6_.m_force.y);
  167.                _loc6_.m_angularVelocity += param1.dt * _loc6_.m_invI * _loc6_.m_torque;
  168.                _loc6_.m_force.SetZero();
  169.                _loc6_.m_torque = 0;
  170.                _loc6_.m_linearVelocity.Multiply(b2Math.b2Clamp(1 - param1.dt * _loc6_.m_linearDamping,0,1));
  171.                _loc6_.m_angularVelocity *= b2Math.b2Clamp(1 - param1.dt * _loc6_.m_angularDamping,0,1);
  172.                if(_loc6_.m_linearVelocity.LengthSquared() > b2Settings.b2_maxLinearVelocitySquared)
  173.                {
  174.                   _loc6_.m_linearVelocity.Normalize();
  175.                   _loc6_.m_linearVelocity.x *= b2Settings.b2_maxLinearVelocity;
  176.                   _loc6_.m_linearVelocity.y *= b2Settings.b2_maxLinearVelocity;
  177.                }
  178.                if(_loc6_.m_angularVelocity * _loc6_.m_angularVelocity > b2Settings.b2_maxAngularVelocitySquared)
  179.                {
  180.                   if(_loc6_.m_angularVelocity < 0)
  181.                   {
  182.                      _loc6_.m_angularVelocity = -b2Settings.b2_maxAngularVelocity;
  183.                   }
  184.                   else
  185.                   {
  186.                      _loc6_.m_angularVelocity = b2Settings.b2_maxAngularVelocity;
  187.                   }
  188.                }
  189.             }
  190.             _loc5_++;
  191.          }
  192.          (_loc8_ = new b2ContactSolver(param1,m_contacts,m_contactCount,m_allocator)).InitVelocityConstraints(param1);
  193.          _loc5_ = 0;
  194.          while(_loc5_ < m_jointCount)
  195.          {
  196.             (_loc7_ = m_joints[_loc5_]).InitVelocityConstraints(param1);
  197.             _loc5_++;
  198.          }
  199.          _loc5_ = 0;
  200.          while(_loc5_ < param1.maxIterations)
  201.          {
  202.             _loc8_.SolveVelocityConstraints();
  203.             _loc9_ = 0;
  204.             while(_loc9_ < m_jointCount)
  205.             {
  206.                (_loc7_ = m_joints[_loc9_]).SolveVelocityConstraints(param1);
  207.                _loc9_++;
  208.             }
  209.             _loc5_++;
  210.          }
  211.          _loc8_.FinalizeVelocityConstraints();
  212.          _loc5_ = 0;
  213.          while(_loc5_ < m_bodyCount)
  214.          {
  215.             if(!(_loc6_ = m_bodies[_loc5_]).IsStatic())
  216.             {
  217.                _loc6_.m_sweep.c0.SetV(_loc6_.m_sweep.c);
  218.                _loc6_.m_sweep.a0 = _loc6_.m_sweep.a;
  219.                _loc6_.m_sweep.c.x += param1.dt * _loc6_.m_linearVelocity.x;
  220.                _loc6_.m_sweep.c.y += param1.dt * _loc6_.m_linearVelocity.y;
  221.                _loc6_.m_sweep.a += param1.dt * _loc6_.m_angularVelocity;
  222.                _loc6_.SynchronizeTransform();
  223.             }
  224.             _loc5_++;
  225.          }
  226.          if(param3)
  227.          {
  228.             _loc5_ = 0;
  229.             while(_loc5_ < m_jointCount)
  230.             {
  231.                (_loc7_ = m_joints[_loc5_]).InitPositionConstraints();
  232.                _loc5_++;
  233.             }
  234.             m_positionIterationCount = 0;
  235.             while(m_positionIterationCount < param1.maxIterations)
  236.             {
  237.                _loc10_ = _loc8_.SolvePositionConstraints(b2Settings.b2_contactBaumgarte);
  238.                _loc11_ = true;
  239.                _loc5_ = 0;
  240.                while(_loc5_ < m_jointCount)
  241.                {
  242.                   _loc12_ = (_loc7_ = m_joints[_loc5_]).SolvePositionConstraints();
  243.                   _loc11_ &&= _loc12_;
  244.                   _loc5_++;
  245.                }
  246.                if(_loc10_ && _loc11_)
  247.                {
  248.                   break;
  249.                }
  250.                ++m_positionIterationCount;
  251.             }
  252.          }
  253.          Report(_loc8_.m_constraints);
  254.          if(param4)
  255.          {
  256.             _loc13_ = Number.MAX_VALUE;
  257.             _loc14_ = b2Settings.b2_linearSleepTolerance * b2Settings.b2_linearSleepTolerance;
  258.             _loc15_ = b2Settings.b2_angularSleepTolerance * b2Settings.b2_angularSleepTolerance;
  259.             _loc5_ = 0;
  260.             while(_loc5_ < m_bodyCount)
  261.             {
  262.                if((_loc6_ = m_bodies[_loc5_]).m_invMass != 0)
  263.                {
  264.                   if((_loc6_.m_flags & b2Body.e_allowSleepFlag) == 0)
  265.                   {
  266.                      _loc6_.m_sleepTime = 0;
  267.                      _loc13_ = 0;
  268.                   }
  269.                   if((_loc6_.m_flags & b2Body.e_allowSleepFlag) == 0 || _loc6_.m_angularVelocity * _loc6_.m_angularVelocity > _loc15_ || b2Math.b2Dot(_loc6_.m_linearVelocity,_loc6_.m_linearVelocity) > _loc14_)
  270.                   {
  271.                      _loc6_.m_sleepTime = 0;
  272.                      _loc13_ = 0;
  273.                   }
  274.                   else
  275.                   {
  276.                      _loc6_.m_sleepTime += param1.dt;
  277.                      _loc13_ = b2Math.b2Min(_loc13_,_loc6_.m_sleepTime);
  278.                   }
  279.                }
  280.                _loc5_++;
  281.             }
  282.             if(_loc13_ >= b2Settings.b2_timeToSleep)
  283.             {
  284.                _loc5_ = 0;
  285.                while(_loc5_ < m_bodyCount)
  286.                {
  287.                   _loc6_ = m_bodies[_loc5_];
  288.                   _loc6_.m_flags |= b2Body.e_sleepFlag;
  289.                   _loc6_.m_linearVelocity.SetZero();
  290.                   _loc6_.m_angularVelocity = 0;
  291.                   _loc5_++;
  292.                }
  293.             }
  294.          }
  295.       }
  296.       
  297.       public function AddContact(param1:b2Contact) : void
  298.       {
  299.          var _loc2_:* = m_contactCount++;
  300.          m_contacts[_loc2_] = param1;
  301.       }
  302.       
  303.       public function Clear() : void
  304.       {
  305.          m_bodyCount = 0;
  306.          m_contactCount = 0;
  307.          m_jointCount = 0;
  308.       }
  309.       
  310.       public function SolveTOI(param1:b2TimeStep) : void
  311.       {
  312.          var _loc2_:int = 0;
  313.          var _loc3_:b2ContactSolver = null;
  314.          var _loc4_:Number = NaN;
  315.          var _loc5_:b2Body = null;
  316.          var _loc6_:Boolean = false;
  317.          _loc3_ = new b2ContactSolver(param1,m_contacts,m_contactCount,m_allocator);
  318.          _loc2_ = 0;
  319.          while(_loc2_ < param1.maxIterations)
  320.          {
  321.             _loc3_.SolveVelocityConstraints();
  322.             _loc2_++;
  323.          }
  324.          _loc2_ = 0;
  325.          while(_loc2_ < m_bodyCount)
  326.          {
  327.             if(!(_loc5_ = m_bodies[_loc2_]).IsStatic())
  328.             {
  329.                _loc5_.m_sweep.c0.SetV(_loc5_.m_sweep.c);
  330.                _loc5_.m_sweep.a0 = _loc5_.m_sweep.a;
  331.                _loc5_.m_sweep.c.x += param1.dt * _loc5_.m_linearVelocity.x;
  332.                _loc5_.m_sweep.c.y += param1.dt * _loc5_.m_linearVelocity.y;
  333.                _loc5_.m_sweep.a += param1.dt * _loc5_.m_angularVelocity;
  334.                _loc5_.SynchronizeTransform();
  335.             }
  336.             _loc2_++;
  337.          }
  338.          _loc4_ = 0.75;
  339.          _loc2_ = 0;
  340.          while(_loc2_ < param1.maxIterations)
  341.          {
  342.             if(_loc6_ = _loc3_.SolvePositionConstraints(_loc4_))
  343.             {
  344.                break;
  345.             }
  346.             _loc2_++;
  347.          }
  348.          Report(_loc3_.m_constraints);
  349.       }
  350.    }
  351. }
  352.